/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
<template>
    <sf-form ref="form"
             class="password-form"
             :label-width="100">
        <sf-form-item v-if="hideOldPassword">
            <sf-fieldlabel><lang>旧密码</lang></sf-fieldlabel>
            <password-input v-model="oldPassword"
                            :editable="true"
                            :default-width="fieldWidth" />
        </sf-form-item>

        <sf-form-item>
            <sf-fieldlabel><lang>新密码</lang></sf-fieldlabel>
            <password-input v-model="newPassword"
                            :editable="true"
                            :default-width="fieldWidth"
                            :validator="_newPasswordValidator"
                            @input="_onPwdChanged" />
            <sf-fieldtip class="m-l15">
                <lang>密码需满足大写字母，小写字母，数字和特殊字符任意三种组合，且长度在8到32位之间</lang>
            </sf-fieldtip>
        </sf-form-item>

        <sf-form-item>
            <sf-fieldlabel><lang>确认新密码</lang></sf-fieldlabel>
            <password-input ref="confirmPwdInput"
                            v-model="confirmPwd"
                            :editable="true"
                            :default-width="fieldWidth"
                            :validator="_confirmPwdValidator" />
        </sf-form-item>
    </sf-form>
</template>
<style lang="less">
    .password-form{
        .sfv-form-item{
            display: flex !important;
        }
    }
</style>

<script>

import passwordInput from 'src/home/mod_cluster/common/password_item';
import passwordValidator from 'src/util/validator/password_validator';

const FIELD_WIDTH = 280;

export default {

    // props: {
    //     hideOldPassword: {
    //         default:false,
    //         type:Boolean
    //     }
    // },

    components: {
        passwordInput
    },

    data () {
        return {
            hideOldPassword : false,
            oldPassword: '',
            newPassword: '',
            confirmPwd: ''
        };
    },

    computed: {
        fieldWidth () {
            return FIELD_WIDTH;
        }
    },

    methods: {

        _newPasswordValidator (v) {
            if (this.oldPassword === this.newPassword) {
                return _('新密码不能与旧密码相同');
            }
            return passwordValidator(v);
        },


        _confirmPwdValidator (v) {
            if (this.newPassword !== this.confirmPwd) {
                return _('两次输入密码不一致');
            }
            return passwordValidator(v);
        },

        _onPwdChanged () {
            let confirmPwdInput = this.$refs.confirmPwdInput;
            confirmPwdInput.validate();
        },

        getJsonValue () {
            let {  newPassword, oldPassword } = this;
            return {
                oldPassword,
                newPassword
            };
        }
    }
};
</script>
